<%-- 
    Document   : order_user
    Created on : 22.04.2013, 1:20:32
    Author     : Yasya, Kate,Svetlana Shikhutskaya
--%>

<%@page import="entity.Users"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="delta.cart.bean.CartLocal"%>
<%@page import="entity.Product"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<script>

   
    function submitForm() {
        
        var email=document.getElementById("email");
        var subscribed=document.getElementById("subscribed");
        var address=(document.getElementById("address"));
        var valid=true;
        
        if(cost==null){
            alert("Please, choose a warehouse");  
            return false;
        }
        if(cost>99999){
            alert("Sorry, shipping cost is too high.Can't proceed.");
            return false;
            
        }
        document.getElementById("cost").value=cost;
        document.getElementById("duration").value=duration;
                

        if(document.getElementById("productCount").value<1){
            alert('Can not submit the order with empty cart');
            return false;
        }
        if (subscribed.checked && !validateEmail()) {
            valid=false;
        }
        if(address.value.length>300){
            document.getElementById("address_error").style.display="inline"; 
            valid=false;
        }else{
            document.getElementById("address_error").style.display="none";      
        }
        if( address.value==""){
            valid=false;   
    
        }if(valid){
            document.getElementById("mainForm").submit();
    
        }else{
            alert("Please, fill in all fields");
    
        }
    }


    function validateEmail(){
        
        var email=document.getElementById("email");
        
        if(!document.getElementById("subscribed").checked){
            document.getElementById("email_error").style.display="none"; 
        }
        
            if(email.validity.valid && email.value.replace(" ","")!=""){
           
                document.getElementById("email_error").style.display="none";  
                return true;
            }else{
         
            if(document.getElementById("subscribed").checked){
                document.getElementById("email_error").style.display="inline";
            }
            return false;
        }         
    }
</script>
<%! CartLocal cart;
    Users user;%>
<%

    InitialContext ic = new InitialContext();
    Object attr = session.getAttribute("cart");
    if (attr == null) {
        cart = (CartLocal) ic.lookup(
                "java:global/Delta/Delta-ejb/Cart!delta.cart.bean.CartLocal");
        session.setAttribute("cart", cart);
    } else {
        cart = (CartLocal) attr;
    }

    user = (Users) session.getAttribute("user");
    String userAddress;
    String customerName;
    String userEmail;
    if (user != null) {
        userAddress = user.getAddress();
        customerName = user.getFirstName() + " " + user.getLastName();
        userEmail = user.getEMail();
    } else {
        userAddress = "";
        customerName = "";
        userEmail = "";
    }
%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/order_user.css">
             <link rel="stylesheet" type="text/css" href="css/all.css">
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCftQY5EZLAysrfSEyhoZ4syr35lwo_-jY&sensor=false&region=UA&language=en"></script>
        <script src="scripts/jquery-1.8.3.min.js"></script>
        <script src="scripts/gmaps.js"></script>
        <title>Order</title>
    </head>
    <body onload="initialized()">      
        <header>
            <div id ="top_name"> <a href="main.jsp"> <img src="/Delta-war/product/1.jpeg"></a>
                <h1> Order </h1> </div>
           
        </header>
        <div id="main">
            <div id="tabl">
                <table>
                    <TR>
                        <td id="head_table_first"><strong>Product</strong></td>
                        <td id="head_table_second"><strong>Number</strong></td>
                        <td id="head_table_third"><strong>Price</strong></td>
                    </TR>
                    <%for (Product product : cart.getProductList().keySet()) {%>
                    <TR>
                        <td id="colomn_first" ><%=product.getName()%> </td>
                        <td id="colomn_second"><%=cart.getProductList().get(product)%> </td>
                        <td id="colomn_third"><%=cart.getProductList().get(product) * product.getPrice()%> </td>
                    </TR>

                    <%}%>   
                </table>
            </div>

            <div id="total_price">
                <p>Total Price 
                    <input 
                        id="tot-price"
                        type="text" 
                        name="order"
                        style=" background: #fff; "
                        value="<%=cart.getTotalPrice()%>"
                        size="15" 
                        readonly/>
                </p>
            </div>



            <input 
                id="productCount"
                type="hidden"
                value="<%=cart.getCount()%>">

            <form method="POST" 
                  id="mainForm"
                  action="ConfirmOrder"> 
                <div id="info">
                    <div id="label1">
                        <p> Customer name </p> 
                        <p> Address <font color="red">*</font></p>
                        <p> E-mail</p>
                    </div>

                    <div id="text1"> 

                        <p>
                            <input 
                                type="text" 
                                name="Customer name" 
                                value="<%=customerName%>"
                                size="44"/>
                        </p>
                        <p>  <input 
                                type="text" 
                                id="address" onload="checkAddress()" oninput="checkAddress()"
                                maxlength="300"
                                name="Address"
                                value="<%=userAddress%>"
                                placeholder="Enter your address" 
                                size="44" />
                            <span 
                                style="color: red;  font-size: 10pt;" 
                                id="route_error">
                            </span>
                            <span 
                                style="color: red; display:none;  font-size: 10pt;" 
                                id="address_error"> 
                                Address is too long
                            </span></p>

                        <p>
                            <input 
                                id="email"
                                type="email" 
                                name="E-mail"
                                maxlength="100"
                                value="<%=userEmail%>"
                                size="44"
                                oninput="validateEmail()"/>
                            <span 
                                style="color: red; display:none; font-size: 10pt;" 
                                id="email_error"> 
                                Invalid email
                            </span>
                        </p>

                        <input 
                            type="button" id="draw-user-marker"
                            value="Show my address" disabled="true"
                            onclick="codeUserAddress()">
                        <br>

                    </div>
                    <div id="track"> 
                        <input 
                            id="subscribed"
                            name="subscribed" 
                            type="checkbox" 
                         
                            > 
                        Yes, I want to track my order
                    </div>  
                </div>       

                <div id="warehouse">
                    <p> Warehouse</p>

                    <div id="warehouse_info">
                        <p> Name: 
                            <select 
                                onchange="displayDistance()" 
                                id="warehouses" 
                                name="warehouse" 
                                disabled="true"
                                size = 1 >
                            </select> 
                        </p>
                        <p> Distance: 
                            <input 
                                type="text" 
                                id="distance" 
                                name="Distance" 
                                size="18" 
                                readonly/> 
                        </p>
                        <p> Cost: 
                            <input 
                                type="text" 
                                id="cost" 
                                name="Cost" 
                                size="18" 
                                readonly/> 
                        </p>

                        <p> Duration: 
                            <input 
                                type="text" 
                                id="duration" 
                                name="duration" 
                                size="18" 
                                readonly/> 
                        </p>

                        <input 
                            id="draw-route"
                            type="button" 
                            value="Show route" 
                            disabled="true"
                            onclick="drawRoute()"/>
                        <br>
                    </div>

                    <div id="map_canvas">
                    </div>
                </div>
                <div id="down">
                    <div id="pay">
                        <p> Amount to pay:  &nbsp &nbsp  
                            <input 
                                id="amount-pay"
                                type="text" 
                                name="Customer_name" 
                                size="18" 
                                readonly/> 
                        </p>  
                    </div>

                    <input  
                        id="cancel" 
                        type="button"
                        onclick="window.location.href='cart_user.jsp' "
                        value="Cancel">

                    <input
                        id="confirm" 
                        value="Confirm"
                        type="button" 
                        onclick="submitForm()">
                </div>

            </form>
        </div>
                                 <div id="footer">
                                <p>  &#169;All rights reserved</p>
                            </div>
    </body>
</html>
